package club.gskj.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import club.gskj.gulimall.product.entity.AttrEntity;
import club.gskj.gulimall.product.service.AttrAttrgroupRelationService;
import club.gskj.gulimall.product.service.AttrService;
import club.gskj.gulimall.product.service.CategoryService;
import club.gskj.gulimall.product.vo.AttrGroupRelationVo;
import club.gskj.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import club.gskj.gulimall.product.entity.AttrGroupEntity;
import club.gskj.gulimall.product.service.AttrGroupService;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.R;


/**
 * 属性分组
 *
 * @author 康纳-connor
 * @email 2417039231@gmail.com
 * @date 2020-12-27 15:07:43
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///api/product/attrgroup

    /**
     * 根据三级分类id查询对应的所有分组下的所有属性列表
     *
     * @return
     */
    @RequestMapping("/{catId}/withattr")
    public R ListAttrByAttrGroup(@PathVariable("catId") Long catId) {

        // 查询分类下的所有属性分组
        // 查询每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.listAttrByAttrGroupByCatId(catId);

        return R.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@PathVariable("catelogId") Long catelogId,
                  @RequestParam Map<String, Object> params) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        // 查询 catelog的完整路径
        Long[] categoryPath = categoryService.getCateLogPath(catelogId);
        attrGroup.setCateLogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /***
     *  查询 分组下关联的 属性列表
     * @param groupId
     * @return
     */
    @RequestMapping("/{groupId}/attr/relation")
    public R ListAttrGroupRelation(@PathVariable("groupId") Long groupId) {

        List<AttrEntity> attrEntityList = attrService.ListRelationAttr(groupId);

        return R.ok().put("data", attrEntityList);
    }


    ///attr/relation/delete

    /**
     * @param vos 数组
     * @return club.gskj.common.utils.R
     * @author connor-康纳
     * @describe: 移除 分组下关联的 属性信息
     * @date 21.1.31 15:24
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] vos) {

        attrService.deleteAttrRelations(vos);

        return R.ok();
    }

//    6/noattr/relation

    /**
     * @param groupId 分组id
     * @return club.gskj.common.utils.R
     * @author connor-康纳
     * @describe: 查询其他没有被当前分组关联的 属性列表
     * @date 21.1.31 15:33
     */
    @RequestMapping("/{groupId}/noattr/relation")
    public R ListAttrGroupNotRelation(@PathVariable("groupId") Long groupId,
                                      @RequestParam Map<String, Object> params) {

        PageUtils pageUtils = attrService.ListNotRelationAttr(groupId, params);
        return R.ok().put("page", pageUtils);
    }

    //attr/relation

    /***
     * @param voList 集合AttrGroupRelationVo
     * @return club.gskj.common.utils.R
     * @author connor-康纳
     * @describe: 新增属性、分组的关联关系
     * @date 21.1.31 16:44
     */
    @PostMapping("/attr/relation")
    public R addAtteLeation(@RequestBody List<AttrGroupRelationVo> voList) {
        attrAttrgroupRelationService.saveBatch(voList);
        return R.ok();
    }
}


